﻿using System.Reactive;
using System.Reactive.Concurrency;
using CommonEntity.CommonImageEntities;
using CommonEntity.Handlers;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ScienceAppService.Entities;
using ScienceAppService.ModuleInterface;
using ScienceAppService.ModuleService;

namespace ScienceAIApp.ViewModels;
public class ImageContentViewModel : ScienceBaseViewModel
{
    private ISplitService _splitService;
    private IImageShowService _imageShowService;
    private IMarkDrawService _markDrawService;

    [Reactive] public ImageMarkDrawViewModel AxViewModel { get; set; }
    [Reactive] public ImageMarkDrawViewModel SagViewModel { get; set; }
    [Reactive] public ImageMarkDrawViewModel CorViewModel { get; set; }

    public event CommonHandler<string> IsShowBigImageEvent;

    public ImageContentViewModel(ISplitService splitService, IImageShowService imageShowService, IMarkDrawService markDrawService,
    ImageMarkDrawViewModel ax, ImageMarkDrawViewModel sag, ImageMarkDrawViewModel cor)
    {
        _splitService = splitService;
        _imageShowService = imageShowService;
        _markDrawService = markDrawService;

        _imageShowService.FrushImageInfoEvent += FrushImageInfoHandler;
        _splitService.ChooseSeriesEvent += ChooseSeriesHandler;
        _imageShowService.FrushCrosslineMatsEvent += FrushCrosslineMatsHandler;
        _markDrawService.FrushCurrentIndexEvent += FrushCurrentIndexHandler;

        ax.SetImageOrientation(ImageOrientationEnum.TransversePlane);
        sag.SetImageOrientation(ImageOrientationEnum.SagittalPlane);
        cor.SetImageOrientation(ImageOrientationEnum.CoronalPlane);

        AxViewModel = ax;
        SagViewModel = sag;
        CorViewModel = cor;

        AxViewModel.ZoomInOutInteraction!.RegisterHandler(context =>
        {
            IsShowBigImageEvent?.Invoke(context.Input ? "ax-true" : "ax-false");
            context.SetOutput(Unit.Default);
        });

        SagViewModel.ZoomInOutInteraction!.RegisterHandler(context =>
        {
            IsShowBigImageEvent?.Invoke(context.Input ? "sag-true" : "sag-false");
            context.SetOutput(Unit.Default);
        });

        CorViewModel.ZoomInOutInteraction!.RegisterHandler(context =>
        {
            IsShowBigImageEvent?.Invoke(context.Input ? "cor-true" : "cor-false");
            context.SetOutput(Unit.Default);
        });

        #region 十字线

        AxViewModel.CrossLineInteraction!.RegisterHandler(async context =>
        {
            // 获取十字线图像
            var p = new int[] { (int)context.Input[0], (int)context.Input[1], (int)context.Input[2] };
            await _imageShowService.FrushCrossLine(p);
            context.SetOutput(Unit.Default);
        });

        SagViewModel.CrossLineInteraction!.RegisterHandler(async context =>
        {
            // 获取十字线图像
            var p = new int[] { (int)context.Input[0], (int)context.Input[1], (int)context.Input[2] };
            await _imageShowService.FrushCrossLine(p);
            context.SetOutput(Unit.Default);
        });

        CorViewModel.CrossLineInteraction!.RegisterHandler(async context =>
        {
            // 获取十字线图像
            var p = new int[] { (int)context.Input[0], (int)context.Input[1], (int)context.Input[2] };
            await _imageShowService.FrushCrossLine(p);
            context.SetOutput(Unit.Default);
        });

        #endregion

    }

    /// <summary>
    /// 序列选中成功 获取imageInfo
    /// </summary>
    /// <param name="flag"></param>
    private async void ChooseSeriesHandler(bool flag)
    {
        if (!flag) return;

        await _imageShowService.SetImageInfo();
        await _imageShowService.GetTagInfoMat();
    }

    private void FrushImageInfoHandler(ImageInfo info)
    {
        RxApp.MainThreadScheduler.Schedule(async () =>
        {
            AxViewModel.Count = info.Count;
            AxViewModel.CurrentIndex = info.Count / 2;

            SagViewModel.Count = info.Width;
            SagViewModel.CurrentIndex = info.Width / 2;

            CorViewModel.Count = info.Height;
            CorViewModel.CurrentIndex = info.Height / 2;

            await _imageShowService.FrushMats(AxViewModel.CurrentIndex, ImageOrientationEnum.TransversePlane);
            await _imageShowService.FrushMats(SagViewModel.CurrentIndex, ImageOrientationEnum.SagittalPlane);
            await _imageShowService.FrushMats(CorViewModel.CurrentIndex, ImageOrientationEnum.CoronalPlane);
        });
    }

    /// <summary>
    /// 十字线刷新
    /// </summary>
    /// <param name="mats"></param>
    private async void FrushCrosslineMatsHandler(CrossLineMat mats)
    {
        if (mats.AxCrossMat == null) return;

        RxApp.MainThreadScheduler.Schedule(async () =>
        {
            AxViewModel.CrossLineMat = mats.AxCrossMat;
            SagViewModel.CrossLineMat = mats.SagCrossMat;
            CorViewModel.CrossLineMat = mats.CorCrossMat;
        });

        // 刷新图像
        if (mats.Point[0] > 0)
        {
            await _imageShowService.FrushMats(mats.Point[2], ImageOrientationEnum.TransversePlane);
            await _imageShowService.FrushMats(mats.Point[0], ImageOrientationEnum.SagittalPlane);
            await _imageShowService.FrushMats(mats.Point[1], ImageOrientationEnum.CoronalPlane);
        }

    }

    /// <summary>
    /// 图像刷新
    /// </summary>
    /// <param name="obj"></param>
    private async void FrushCurrentIndexHandler(object obj)
    {
        await _imageShowService.FrushMats(AxViewModel.CurrentIndex, ImageOrientationEnum.TransversePlane);
        await _imageShowService.FrushMats(SagViewModel.CurrentIndex, ImageOrientationEnum.SagittalPlane);
        await _imageShowService.FrushMats(CorViewModel.CurrentIndex, ImageOrientationEnum.CoronalPlane);
    }

}
